﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace AutomaticAdvertising
{
    public partial class PostEditor : Form
    {
        private const int k_DefaultDurationOfPostInDays = 7;

        public PostData ReturnValue { get; private set; }

        public PostEditor(string i_DateTimeFormat)
        {
            InitializeComponent();

            dateTimePickerStart.CustomFormat = i_DateTimeFormat;
            dateTimePickerEnd.CustomFormat = i_DateTimeFormat;

            buttonPost.Text = "Publish";

            dateTimePickerStart.Value = DateTime.Now;
            dateTimePickerEnd.Value = dateTimePickerStart.Value.AddDays(k_DefaultDurationOfPostInDays);

            ReturnValue = new PostData();
            ReturnValue.Status = PostData.eStatus.notPosted;
        }

        public PostEditor(PostData i_PostData, string i_DateTimeFormat)
        {
            InitializeComponent();

            dateTimePickerStart.CustomFormat = i_DateTimeFormat;
            dateTimePickerEnd.CustomFormat = i_DateTimeFormat;

            ReturnValue = i_PostData;

            richTextBoxPostInfo.Text = i_PostData.Text;
            
            richTextBoxPostInfo.Enabled = false;
            buttonBrowse.Enabled = false;
            
            buttonPost.Text = "Update";

            if (!string.IsNullOrEmpty(i_PostData.ImageLocalPath))
            {
                pictureBoxPostImagePrivew.ImageLocation = i_PostData.ImageLocalPath;
            }
            else
            {
                pictureBoxPostImagePrivew.LoadAsync(i_PostData.Photo.PictureNormalURL);
            }
            
            dateTimePickerStart.Value = i_PostData.StartDate;
            dateTimePickerEnd.Value = i_PostData.EndDate;
        }

        private void buttonBrowse_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();

            openFileDialog.Filter = "Image Files(*.jpg; *.jpeg)|*.jpg; *.jpeg";

            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                pictureBoxPostImagePrivew.ImageLocation = openFileDialog.FileName; 
            } 
        }

        private void buttonPost_Click(object sender, EventArgs e)
        {
            if (isFormDataValid())
            {
                if (buttonPost.Text == "Update")
                {
                    ReturnValue.StartDate = dateTimePickerStart.Value;
                    ReturnValue.EndDate = dateTimePickerEnd.Value;
                }
                else
                {
                    ReturnValue.Text = richTextBoxPostInfo.Text;
                    ReturnValue.ImageLocalPath = pictureBoxPostImagePrivew.ImageLocation;
                    ReturnValue.StartDate = dateTimePickerStart.Value;
                    ReturnValue.EndDate = dateTimePickerEnd.Value;
                }

                this.Close();
                DialogResult = DialogResult.OK;
            }
            else
            {
                MessageBox.Show("Please fill all data", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private bool isFormDataValid()
        {
            bool isValidate = true;

            isValidate &= !string.IsNullOrEmpty(pictureBoxPostImagePrivew.ImageLocation);
            isValidate &= !string.IsNullOrEmpty(richTextBoxPostInfo.Text);
            isValidate &= dateTimePickerEnd.Value > dateTimePickerStart.Value;

            return isValidate;
        }
    }
}
